$(function() {
  render()
  function getHistory() {
    var history = localStorage.getItem('history')
    var arrHistory = JSON.parse(history)  || []
    return arrHistory
  }
  // getHistory()

  function render() {
    var history = getHistory()
    $('.history').html(template('tmp',{info:history}))
  }

  function putHistory(arr) {
    var str = JSON.stringify(arr)
    localStorage.setItem('history',str)
  }
  $('.btn_search').on('click',function() {
    var value = $('[name=search]').val()
    if(!value) {
      mui.toast('请输入要搜索的内容')
      return 
    }
    var history = getHistory()
    var index = history.indexOf(value)
    if (index > -1) {
      history.splice(index,1)
    }
    if(history.length >= 10 ) {
      history.pop()
    }
    history.unshift(value)
    putHistory(history)
    render()
    location.href = 'searchList.html?key=' + value
  })

  $('.history').on('click','.btn_delete',function() {
    var idx = $(this).data('idx')
    var history = getHistory()
    history.splice(idx, 1)
    putHistory(history)
    render()
  })

  $('.history').on('click','.btn_clear',function() {
    localStorage.removeItem('history')
    render()
  })
})